home *** CD-ROM | disk | FTP | other *** search
- /*
- File: BndNSUtl.cp
-
- Contains: Binding utilities for EditorSetup.
-
- Owned by: Caia Grisar
-
- Copyright: © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
-
-
- */
-
- #ifndef _BNDNSUTL_
- #include "BndNSUtl.h"
- #endif
-
- #ifndef _EDITRSET_
- #include "EditrSet.h"
- #endif
-
- #ifndef _ODDEBUG_
- #include "ODDebug.h"
- #endif
-
- #ifndef _ODMEMORY_
- #include "ODMemory.h"
- #endif
-
- #ifndef SOM_ODValueNameSpace_xh
- #include "ValueNS.xh"
- #endif
-
- #ifndef SOM_ODObjectNameSpace_xh
- #include "ObjectNS.xh"
- #endif
-
- #ifndef _STORAGEU_
- #include "StorageU.xh"
- #endif
-
- #ifndef _STDDEFS_
- #include "StdDefs.xh"
- #endif
-
- #ifndef _STDPROPS_
- #include "StdProps.xh"
- #endif
-
- #ifndef _STDTYPES_
- #include "StdTypes.xh"
- #endif
-
- #ifndef _PART_
- #include "Part.xh"
- #endif
-
- #ifndef SOM_ODValueIterator_xh
- #include <ValueItr.xh>
- #endif
-
- #ifndef SOM_ODObjectIterator_xh
- #include <ObjctItr.xh>
- #endif
-
- #ifndef _ISOSTR_
- #include "ISOStr.h"
- #endif
-
- #ifndef SOM_ODStorageUnitView_xh
- #include <SUView.xh>
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef _ODUTILS_
- #include <ODUtils.h>
- #endif
-
- #ifndef SOM_ODNameSpaceManager_xh
- #include <NmSpcMg.xh>
- #endif
-
- #ifndef SOM_ODTypeList_xh
- #include <TypeList.xh>
- #endif
-
- #ifndef _STORUTIL_
- #include <StorUtil.h>
- #endif
-
- #ifndef _NMSPCUTL_
- #include <NmSpcUtl.h>
- #endif
-
- #ifndef _UTILERRS_
- #include <UtilErrs.h>
- #endif
-
- #ifndef SOM_ODTypeListIterator_xh
- #include <TypLsItr.xh>
- #endif
-
- #include <somcls.xh>
- #include <somcm.xh>
-
-
- //------------------------------------------------------------------------------
- // GetSysPrefEditorForKind
- //------------------------------------------------------------------------------
- // Returns the system-wide preferred editor for the argument data kind.
- // A null value is returned if no preference has been specified.
- // This routine checks to ensure that the preferred editor actually exists;
- // if it does not, the preference is canceled and null is returned.
-
- ODEditor GetSysPrefEditorForKind(ODNameSpaceManager* theNmSpcMgr,
- ODType kind)
- {
- ODEditor theEditor = kODNULL;
- ODULong valueLen;
- ODBoolean sysPrefEditorFound = kODFalse;
-
- Environment* ev = somGetGlobalEnvironment();
-
- // look it up in the editorkinds namespace
- ODValueNameSpace* sysPrefEditorKindsNameSpace =
- (ODValueNameSpace*)theNmSpcMgr->HasNameSpace( ev,
- kODSysPrefEditorKinds );
- if (sysPrefEditorKindsNameSpace)
- {
- sysPrefEditorFound = ValueNameSpaceGetEntry(
- sysPrefEditorKindsNameSpace, ev, kind,
- (ODPtr*) &theEditor, &valueLen );
- }
- return theEditor;
- }
-
- //------------------------------------------------------------------------------
- // GetSysPrefEditorForCategory
- //------------------------------------------------------------------------------
- ODEditor GetSysPrefEditorForCategory(ODNameSpaceManager* theNmSpcMgr,
- ODType category)
- {
- ODEditor theEditor = kODNULL;
- ODULong valueLen;
- ODBoolean sysPrefEditorFound = kODFalse;
-
- Environment* ev = somGetGlobalEnvironment();
-
- // look it up in the editorkinds namespace
- ODValueNameSpace* sysPrefEditorCategoriesNameSpace =
- (ODValueNameSpace*)theNmSpcMgr->HasNameSpace( ev,
- kODSysPrefEditorCategories );
-
- if (sysPrefEditorCategoriesNameSpace)
- {
- sysPrefEditorFound = ValueNameSpaceGetEntry(
- sysPrefEditorCategoriesNameSpace, ev, category,
- (ODPtr*) &theEditor, &valueLen );
- }
- return theEditor;
- }
-
- //------------------------------------------------------------------------------
- // SetSysPrefEditorForKind
- //------------------------------------------------------------------------------
- ODEditor SetSysPrefEditorForKind(ODNameSpaceManager* theNmSpcMgr,
- ODType kind, ODEditor editorString)
- {
- ODEditor editor = kODNULL;
-
- Environment* ev = somGetGlobalEnvironment();
-
- // look it up in the editorkinds namespace
- ODValueNameSpace* sysPrefEditorKindsNameSpace =
- (ODValueNameSpace*)theNmSpcMgr->HasNameSpace( ev,
- kODSysPrefEditorKinds );
-
- if (sysPrefEditorKindsNameSpace)
- {
- ODULong strLength = ODISOStrLength((const ODISOStr)editorString);
- editor = (ODEditor)ODNewPtrClear(strLength+1, kDefaultHeapID);
- ODISOStrNCopy((ODISOStr)editor,(const ODISOStr)editorString, strLength);
- ValueNameSpaceRegister( sysPrefEditorKindsNameSpace, ev, (ODISOStr)kind,
- (ODPtr)editor, strLength+1 );
- theNmSpcMgr->UpdatePreferences(ev);
- }
-
- return editor;
- }
-
- //------------------------------------------------------------------------------
- // SetSysPrefEditorForCategory
- //------------------------------------------------------------------------------
- ODEditor SetSysPrefEditorForCategory(ODNameSpaceManager* theNmSpcMgr,
- ODType category,ODEditor editorString)
- {
- ODEditor editor = kODNULL;
-
- Environment* ev = somGetGlobalEnvironment();
-
- // look it up in the editorkinds namespace
- ODValueNameSpace* sysPrefEditorCategoriesNameSpace =
- (ODValueNameSpace*)theNmSpcMgr->HasNameSpace( ev,
- kODSysPrefEditorCategories );
-
- if (sysPrefEditorCategoriesNameSpace)
- {
- ODULong strLength = ODISOStrLength((const ODISOStr)editorString);
- editor = (ODEditor)ODNewPtrClear(strLength+1, kDefaultHeapID);
- ODISOStrNCopy((ODISOStr)editor,(const ODISOStr)editorString, strLength);
- ValueNameSpaceRegister( sysPrefEditorCategoriesNameSpace, ev,
- (ODISOStr)category, (ODPtr)editor, strLength+1);
- theNmSpcMgr->UpdatePreferences(ev);
- }
-
- return editor;
- }
-
- //------------------------------------------------------------------------------
- // GetCategoriesOfKind
- //
- // lookup kind in kindcategory namespace and return categories supported
- //------------------------------------------------------------------------------
-
- ODTypeList* GetCategoriesOfKind(ODNameSpaceManager* theNmSpcMgr,
- ODType kind)
- {
- ODTypeList* categoryList = kODNULL;
-
- Environment* ev = somGetGlobalEnvironment();
-
- // look it up in the kindcategory namespace
- ODObjectNameSpace* kindCategoryNameSpace =
- (ODObjectNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODKind );
-
- if (kindCategoryNameSpace)
- {
- if (kindCategoryNameSpace->GetEntry(ev, (ODISOStr)kind,
- (ODObject**)&categoryList ))
- return categoryList;
- else
- return kODNULL;
- }
- return categoryList;
- }
-
-
- //------------------------------------------------------------------------------
- // GetUserKindFromKind
- //------------------------------------------------------------------------------
- ODBoolean GetUserKindFromKind( ODNameSpaceManager* theNmSpcMgr,
- ODType kind, ODName** name )
- {
- ODBoolean result = kODFalse ;
-
- Environment* ev = somGetGlobalEnvironment();
-
- // look it up in the spaceName namespace
- ODValueNameSpace* userStringNameSpace =
- (ODValueNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODKindUserString );
-
- if (userStringNameSpace)
- result = ValueNameSpaceGetODName(userStringNameSpace, ev, kind, name );
- return result ;
- }
-
-
- //------------------------------------------------------------------------------
- // GetUserEditorFromEditor
- //------------------------------------------------------------------------------
- ODBoolean GetUserEditorFromEditor( ODNameSpaceManager* theNmSpcMgr,
- ODEditor editor, ODName** name )
- {
- ODBoolean result = kODFalse ;
-
- Environment* ev = somGetGlobalEnvironment();
-
- // look it up in the spaceName namespace
- ODValueNameSpace* userStringNameSpace =
- (ODValueNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODEditorUserString );
-
- if (userStringNameSpace)
- result = ValueNameSpaceGetODName(userStringNameSpace, ev, editor, name );
- return result ;
- }
-
-
- //------------------------------------------------------------------------------
- // GetUserCatFromCat
- //------------------------------------------------------------------------------
- ODBoolean GetUserCatFromCat( ODNameSpaceManager* theNmSpcMgr,
- ODType category, ODName** name )
- {
- ODBoolean result = kODFalse ;
-
- Environment* ev = somGetGlobalEnvironment();
-
- // look it up in the spaceName namespace
- ODValueNameSpace* userStringNameSpace =
- (ODValueNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODCategoryUserString );
-
- if (userStringNameSpace)
- result = ValueNameSpaceGetODName(userStringNameSpace, ev, category, name );
- return result ;
- }
-
- //------------------------------------------------------------------------------
- // ODGetODOSTypeForKind
- //------------------------------------------------------------------------------
- ODOSType ODGetODOSTypeForKind(ODNameSpaceManager* theNmSpcMgr,
- ODType kind)
- {
- ODOSType theOSType = kODNULL;
-
- Environment* ev = somGetGlobalEnvironment();
-
- // look it up in the editorkinds namespace
- ODValueNameSpace* kindOldMacOSTypeNameSpace =
- (ODValueNameSpace*)theNmSpcMgr->HasNameSpace( ev,
- kODKindOldMacOSType );
-
- if (kindOldMacOSTypeNameSpace)
- {
- ODOSType* typePtr;
- ODULong valueLen;
- if (ValueNameSpaceGetEntry(
- kindOldMacOSTypeNameSpace, ev, kind,
- (ODPtr*) &typePtr, &valueLen ))
- {
- theOSType = *typePtr;
- ODDeleteObject( typePtr );
- }
- }
- return theOSType;
- }
-
- //------------------------------------------------------------------------------
- // EditorSupportsKind
- //------------------------------------------------------------------------------
- ODBoolean EditorSupportsKind( ODNameSpaceManager* theNmSpcMgr,
- ODEditor editor, ODType kind )
- {
- ODTypeList* kindsList = kODNULL;
- Environment* ev = somGetGlobalEnvironment();
-
- // get the editorkinds namespace
- ODObjectNameSpace* editorkindsNameSpace =
- (ODObjectNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODEditorKinds );
-
- if (editorkindsNameSpace)
- {
- // Lookup the kinds list by editor class ID
- if (!editorkindsNameSpace->GetEntry( ev, (ODISOStr)editor,
- (ODObject**)&kindsList ))
- kindsList = (ODTypeList*)kODNULL;
- }
-
- return (kindsList && kindsList->Contains(ev, kind));
- }
-
-
- //------------------------------------------------------------------------------
- // GetAllEditorsForCategory
- //
- // lookup kinds in editorkinds namespace and return editors supported
- //------------------------------------------------------------------------------
-
- ODBoolean GetAllEditorsForCategory( ODNameSpaceManager* theNmSpcMgr,
- ODType category, EditorSet* theEditorSet )
- {
- ODTypeList* categoriesList;
- ODBoolean retval = kODFalse;
- ODULong len;
-
- Environment* ev = somGetGlobalEnvironment();
-
- ODObjectNameSpace* kindCategories =
- (ODObjectNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODKind ) ;
- if ( kindCategories )
- {
- ODObjectIterator* iter = kindCategories->CreateIterator(ev) ;
- ODType kind ;
-
- for (iter->First(ev, (ODISOStr*)&kind, (ODObject**)&categoriesList, &len);
- iter->IsNotComplete(ev);
- iter->Next(ev, (ODISOStr*)&kind, (ODObject**)&categoriesList, &len))
- {
- if ( categoriesList && categoriesList->Contains( ev, category ) )
- {
- EditorSet* thisEditorSet = new EditorSet() ;
- thisEditorSet->InitEditorSet();
- if ( GetAllEditorsForKind( theNmSpcMgr,
- kind, thisEditorSet ) )
- {
- theEditorSet->AddEditorSet( thisEditorSet ) ;
- retval = kODTrue;
- }
- ODDeleteObject( thisEditorSet );
- }
- }
- ODDeleteObject( iter );
- }
- return retval ;
- }
-
-
- //------------------------------------------------------------------------------
- // GetAllEditorsForKind
- //
- // lookup kinds in editorkinds namespace and return editors supported
- //------------------------------------------------------------------------------
-
- ODBoolean GetAllEditorsForKind(ODNameSpaceManager* theNmSpcMgr,
- ODType kind, EditorSet* theEditorSet)
- {
- ODTypeList* kindsList;
- ODEditor editor;
- ODBoolean retval = kODFalse;
- ODULong len;
-
- Environment* ev = somGetGlobalEnvironment();
-
- if (theEditorSet)
- {
- // get the editorkinds namespace
- ODObjectNameSpace* editorkindsNameSpace =
- (ODObjectNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODEditorKinds );
-
- if (editorkindsNameSpace)
- {
- // iterate through editorKinds namespace
- ODObjectIterator* iter = editorkindsNameSpace->CreateIterator(ev) ;
- for (iter->First(ev, (ODISOStr*)&editor, (ODObject**)&kindsList, &len);
- iter->IsNotComplete(ev);
- iter->Next(ev, (ODISOStr*)&editor, (ODObject**)&kindsList, &len))
- {
- if ( kindsList )
- {
- if ( kindsList->Contains(ev, kind) )
- {
- theEditorSet->AddEditor(editor);
- retval = kODTrue;
- }
- ODDisposePtr( editor );
- }
- }
-
- ODDeleteObject( iter );
- }
- }
- return retval;
- }
-
- //------------------------------------------------------------------------------
- // GetAllKindsForEditor
- //------------------------------------------------------------------------------
-
- void GetAllKindsForEditor(ODNameSpaceManager* theNmSpcMgr,
- ODTypeList* kindsList, ODEditor editor)
- {
- ODType kind = kODNULL;
- ODTypeList* nsKindsList = kODNULL;
-
- Environment* ev = somGetGlobalEnvironment();
-
- if (kindsList)
- {
- // get the editorkinds namespace
- ODObjectNameSpace* editorkindsNameSpace =
- (ODObjectNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODEditorKinds );
-
- if (editorkindsNameSpace)
- {
- if(editorkindsNameSpace->GetEntry( ev, (ODType)editor,
- (ODObject**)&nsKindsList ))
- {
- ODTypeListIterator* iter = nsKindsList->
- CreateTypeListIterator(ev);
- for (kind = iter->First(ev);
- iter->IsNotComplete(ev);
- kind = iter->Next(ev))
- {
- kindsList->AddLast(ev, kind);
- ODDisposePtr(kind);
- }
-
- ODDeleteObject( iter );
- }
- }
- }
- }
-
- //------------------------------------------------------------------------------
- // ReadSize
- //------------------------------------------------------------------------------
-
- ODULong ReadSize(ODFileRefNum file, ODStorageUnitView* view)
- {
- ODULong data;
- ODSLong length = sizeof(data);
- ODSLong savedLength = length;
- Environment* ev = somGetGlobalEnvironment ();
-
- if (view)
- length = StorageUnitViewGetValue(view, ev, length, &data);
- else
- THROW_IF_ERROR(FSRead(file, &length, (long *)&data));
-
- if (savedLength != length)
- THROW(kODErrReadErr);
-
- return data;
- }
-
- //------------------------------------------------------------------------------
- // ReadBytes
- //------------------------------------------------------------------------------
-
- void ReadBytes(ODFileRefNum file, ODStorageUnitView* view,
- ODPtr bytes, ODULong* length)
- {
- ODSLong savedLength = *length;
- Environment* ev = somGetGlobalEnvironment ();
-
- if (view)
- *length = StorageUnitViewGetValue(view, ev, *length, bytes);
- else
- THROW_IF_ERROR(FSRead(file, (long *)length, bytes));
-
- if (savedLength != (ODSLong)*length)
- THROW(kODErrReadErr);
- }
-
-
- //------------------------------------------------------------------------------
- // ODGetEditorSetFromResFile
- //------------------------------------------------------------------------------
-
- EditorSet* ODGetEditorSetFromResFile(ODSShort resRefNum)
- {
- EditorSet* theEditorSet = kODNULL;
- ODBoolean editorFound = kODFalse;
-
- if( resRefNum != -1 ) {
- TRY{
- LOG("* Reading 'nmap's from file %hd\n",resRefNum);
- ODSShort nmapCount = Count1Resources( kODNameMappings );
- for (short i=1; i<=nmapCount; i++)
- {
- // Get the Resource
- ODHandle nmap = (ODHandle)Get1IndResource(kODNameMappings, i);
- ODError result = ResError();
-
- if ( nmap != nil && !result )
- {
- ODLockHandle((ODHandle) nmap);
-
- theEditorSet = ODGetEditorSetFromNMAP(nmap);
-
- ReleaseResource( nmap );
- }
-
- if (theEditorSet)
- break;
- }
- }CATCH_ALL{
- WARN("Error %ld reading nmaps for file %hd",ErrorCode(),resRefNum);
- }ENDTRY
- }
- return theEditorSet;
- }
-
- //------------------------------------------------------------------------------
- // ODGetEditorSetFromNMAP
- //------------------------------------------------------------------------------
-
- EditorSet* ODGetEditorSetFromNMAP( ODHandle nmap )
- {
- ODISOStr nameSpaceName = kODNULL; ODVolatile(nameSpaceName);
- ODUShort nameLen;
- ODISOStr editor;
- ODUShort editorLen;
- ODSize bufferPos=(ODSize)*nmap;
- ODUShort editorCount;
- ODSize sizeShort = sizeof(ODUShort);
- Environment* ev = somGetGlobalEnvironment ();
- EditorSet* theEditorSet = kODNULL;
-
- ODBlockMove((ODPtr)bufferPos, (ODPtr)&nameLen, sizeShort);
- bufferPos += sizeShort;
-
- TRY
- nameSpaceName = (ODISOStr)ODNewPtrClear((ODSize)nameLen+1, kDefaultHeapID);
- ODBlockMove((ODPtr)bufferPos, (ODPtr)nameSpaceName, (ODSize)nameLen);
- bufferPos += (ODSize)nameLen;
-
- if (ODISOStrEqual( nameSpaceName, kODEditorUserString ))
- {
- ODBlockMove((ODPtr)bufferPos, (ODPtr)&editorCount, sizeShort);
- bufferPos += sizeShort;
-
- if ( editorCount > 0 )
- {
- theEditorSet = new EditorSet() ;
- theEditorSet->InitEditorSet();
-
- for(ODUShort i=0; i < editorCount; i++)
- {
- ODBlockMove((ODPtr)bufferPos, (ODPtr)&editorLen, sizeShort);
- bufferPos += sizeShort;
-
- editor = (ODISOStr)ODNewPtrClear((ODSize)editorLen+1, kDefaultHeapID);
- ODBlockMove((ODPtr)bufferPos, (ODPtr)editor, (ODSize)editorLen);
- bufferPos += (ODSize)editorLen;
-
- theEditorSet->AddEditor(editor);
-
- // skip over the corresponding UserString in the nmap
- bufferPos += sizeShort;
- bufferPos += sizeof(ScriptCode);
- bufferPos += sizeof(LangCode);
- ODBlockMove((ODPtr)bufferPos, (ODPtr)&nameLen, sizeShort);
- bufferPos += sizeShort;
- bufferPos += nameLen;
- }
- }
- }
- CATCH_ALL
- WARN("Error %ld in Read_nmap_buffer",ErrorCode());
- ENDTRY
- ODDisposePtr((ODPtr)nameSpaceName);
- return theEditorSet;
- }
-
-